package com.gxuwz.Market.business.action.web.front;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.Market.business.entity.ServiceData;
import com.gxuwz.Market.business.entity.ServiceTask;
import com.gxuwz.Market.business.entity.SysRole;
import com.gxuwz.Market.business.entity.SysRoleRight;
import com.gxuwz.Market.business.entity.SysUser;
import com.gxuwz.Market.business.entity.SysUserRole;
import com.gxuwz.Market.business.service.IServiceDataService;
import com.gxuwz.Market.business.service.IServiceTaskService;
import com.gxuwz.Market.business.service.ISysRoleRightService;
import com.gxuwz.Market.business.service.ISysUserRoleService;
import com.gxuwz.Market.business.service.ISysUserService;
import com.gxuwz.core.web.action.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import util.MyTime;

public class LoginAction extends BaseAction implements Preparable, ModelDriven{
	protected static final String LOGIN_JSP = "/loginBiHuXueChe.jsp";
	protected static final String MAIN_JSP = "/WEB-INF/page/main.jsp";
	protected static final String TOP_JSP = "/WEB-INF/page/top.jsp";
	protected static final String LEFT_JSP = "/WEB-INF/page/left.jsp";
	protected static final String INDEX_JSP = "/WEB-INF/page/index.jsp";
	protected static final String TEXT_JSP = "/WEB-INF/page/Test.jsp";

	
	
	@Autowired
	private IServiceTaskService serviceTaskService;
	@Autowired
	private IServiceDataService serviceDataService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private ISysUserRoleService sysUserRoleService;
	@Autowired
	private ISysRoleRightService sysRoleRightService;
	private SysUser sysUser;
	private List<String> listRight;
	private List<ServiceTask> taskList = new ArrayList<ServiceTask>();
	
	private String uname;
	private String upwd;
	
	
	
	@Override
	public Object getModel() {
		return sysUser;
	}
	@Override
	public void prepare() throws Exception {
		if(null == sysUser){
			sysUser = new SysUser();
		}
	}
	
	
	public String webLogin(){
		boolean isLogin = false;
		isLogin = sysUserService.isLogin(sysUser);
		if(isLogin) {
			SysUser su = sysUserService.findByUserId(sysUser.getUserId());
			listRight = new ArrayList<String>();
			List<SysUserRole> roleList = sysUserRoleService.findByProperty("userId", su.getUserId());
			for(SysUserRole role:roleList){
				List<SysRoleRight> rightList = sysRoleRightService.findByProperty("roleId", role.getRoleId());
				for(SysRoleRight right:rightList){
					listRight.add(right.getRightId());
				}
			}
			
		//	listRight=sysUserService.findUserPermissions(sysUser.getUserId());
			
//			for(String s:listRight){
//				System.out.println("权限"+s);
//			}
			
			//把用户权限放到session当中
			ActionContext.getContext().getSession().put("listRight", listRight);
			
			ActionContext.getContext().getSession().put("sysUser", sysUser);
			getRequest().getSession().setAttribute("userId",sysUser.getUserId() );
			forwardView = MAIN_JSP;
			
		}else{
			System.out.println("失败");
			forwardView = LOGIN_JSP;
		}
		
		
		
		
		return SUCCESS;
	}
	/**
	 * 登录-顶部跳转
	 * @return
	 */
	public String openTop(){
		forwardView = TOP_JSP;
		return SUCCESS;
	}

	/**
	 * 登录-顶部跳转
	 * @return
	 */
	public String openLeft(){
		forwardView = LEFT_JSP;
		return SUCCESS;
	}

	public String openIndex(){
		//获取登录人id
		String userId = (String)getRequest().getSession().getAttribute("userId");
		//查询状态为未完成的任务
		List<ServiceTask> tList = serviceTaskService.findByState(0);
		if(tList != null && tList.size()>0){
			for(ServiceTask t: tList){
				ServiceData sd = serviceDataService.findByWriterId(t.getDataName(), userId, 0);
				
				if(sd != null){
					taskList.add(t);
				}
			}
		}

		forwardView = INDEX_JSP;
		return SUCCESS;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public SysUser getSysUser() {
		return sysUser;
	}
	public void setSysUser(SysUser sysUser) {
		this.sysUser = sysUser;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getUpwd() {
		return upwd;
	}
	public void setUpwd(String upwd) {
		this.upwd = upwd;
	}
	public List<ServiceTask> getTaskList() {
		return taskList;
	}
	public void setTaskList(List<ServiceTask> taskList) {
		this.taskList = taskList;
	}
	
	
	
	
}
